package cn.codergege.kotlinclass.classandinheritance

class Fish(val name: String, val age: Int?)

// 主构造函数中不加 var, val, 那么 name 和 age 只是构造函数的参数而已
class Animal constructor(name: String, age:Int?)

// 如果不加 var, val
// 需要再写一下属性初始化代码才行
class Dog (name: String, age: Int?) {
    val name = name
    val age = age
    // 属性必须初始化, 除非使用 lateinit var 标记为延迟初始化的属性
    lateinit var owner: String
}

class Person(val name: String, val age: Int?) {
    init {
        println("init")
        require(name.trim().length > 0) {
            "名称长度必须大于 0 !"
        }
        if(age != null) {
            require(age > 0 && age < 150) {
                "年龄必须是 (0, 150) 之间"
            }
        }
    }
    // 非主构造函数中, 参数不允许有 val 和 var
    constructor(name: String): this(name, null)
}

class InitOrderDemo(name: String) {
    val firstProperty = "First property: $name".also(::println)

    init {
        println("First initializer block that prints ${name}")
    }

    val secondProperty = "Second property: ${name.length}".also(::println)

    init {
        println("Second initializer block that prints ${name.length}")
    }
}

class Secondary(name: String){
    val name = name
    constructor(name: String, nickname:String): this(name = ""){
        println("次构造函数")
    }
}

class Cat(val name: String) {
    var age: Int = 0
    constructor(name: String, age: Int) : this(name) {
        this.age = age
    }
}
